<!DOCTYPE html>





<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 3.9.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/favicon.ico?v=7.4.0">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.ico?v=7.4.0">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon.ico?v=7.4.0">
  <link rel="mask-icon" href="/images/logo.svg?v=7.4.0" color="#222">

<link rel="stylesheet" href="/css/main.css?v=7.4.0">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.7.0">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '7.4.0',
    exturl: false,
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false,"dimmer":false},
    copycode: {"enable":false,"show_result":false,"style":null},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    bookmark: {"enable":false,"color":"#222","save":"auto"},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: 'search.json',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    translation: {
      copy_button: '复制',
      copy_success: '复制成功',
      copy_failure: '复制失败'
    },
    sidebarPadding: 40
  };
</script>

  <meta name="description" content="1. Makefile 介绍Makefile文件由一系列规则（rules）构成。每条规则的形式如下。 12&amp;lt;target&amp;gt; : &amp;lt;prerequisites&amp;gt; [tab]  &amp;lt;commands&amp;gt;  上面第一行冒号前面的部分，叫做”目标”（target），冒号后面的部分叫做”前置条件”（prerequisites）；第二行必须由一个tab键起首，后面跟着”命令”（">
<meta name="keywords" content="makefile">
<meta property="og:type" content="article">
<meta property="og:title" content="Makefile的编写规则">
<meta property="og:url" content="https://unix2dos.github.io/p/4cf47ff4.html">
<meta property="og:site_name" content="Levon&#39;s Blog">
<meta property="og:description" content="1. Makefile 介绍Makefile文件由一系列规则（rules）构成。每条规则的形式如下。 12&amp;lt;target&amp;gt; : &amp;lt;prerequisites&amp;gt; [tab]  &amp;lt;commands&amp;gt;  上面第一行冒号前面的部分，叫做”目标”（target），冒号后面的部分叫做”前置条件”（prerequisites）；第二行必须由一个tab键起首，后面跟着”命令”（">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-11-21T14:30:53.145Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Makefile的编写规则">
<meta name="twitter:description" content="1. Makefile 介绍Makefile文件由一系列规则（rules）构成。每条规则的形式如下。 12&amp;lt;target&amp;gt; : &amp;lt;prerequisites&amp;gt; [tab]  &amp;lt;commands&amp;gt;  上面第一行冒号前面的部分，叫做”目标”（target），冒号后面的部分叫做”前置条件”（prerequisites）；第二行必须由一个tab键起首，后面跟着”命令”（">
  <link rel="canonical" href="https://unix2dos.github.io/p/4cf47ff4">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true,
    isPage: false,
    isArchive: false
  };
</script>

  <title>Makefile的编写规则 | Levon's Blog</title>
  








  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">
  <div class="container use-motion">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Levon's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <h1 class="site-subtitle" itemprop="description">微信:  L6241425</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
      
      
      
        
        <li class="menu-item menu-item-home">
      
    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-archives">
      
    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>时光</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-categories">
      
    

    <a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-tags">
      
    

    <a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>

  </li>
      <li class="menu-item menu-item-search">
        <a href="javascript:;" class="popup-trigger">
        
          <i class="menu-item-icon fa fa-search fa-fw"></i> <br>搜索</a>
      </li>
    
  </ul>

</nav>
  <div class="site-search">
    <div class="popup search-popup">
    <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocorrect="off" autocapitalize="none"
           placeholder="搜索..." spellcheck="false"
           type="text" id="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result"></div>

</div>
<div class="search-pop-overlay"></div>

  </div>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
            

          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
      <article itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block post">
    <link itemprop="mainEntityOfPage" href="https://unix2dos.github.io/p/4cf47ff4.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Levon">
      <meta itemprop="description" content="Never Give Up">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Levon's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">Makefile的编写规则

          
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              
                
              

              <time title="创建时间：2019-11-21 21:11:46 / 修改时间：22:30:53" itemprop="dateCreated datePublished" datetime="2019-11-21T21:11:46+08:00">2019-11-21</time>
            </span>
          
            

            
          
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/2-linux系统/" itemprop="url" rel="index"><span itemprop="name">2-linux系统</span></a></span>

                
                
                  ，
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/2-linux系统/shell/" itemprop="url" rel="index"><span itemprop="name">shell</span></a></span>

                
                
              
            </span>
          

          
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
        
      
      <span class="post-meta-item-text">Disqus：</span>
    
    <a title="disqus" href="/p/4cf47ff4.html#comments" itemprop="discussionUrl"><span class="post-comments-count disqus-comment-count" data-disqus-identifier="p/4cf47ff4.html" itemprop="commentCount"></span></a>
  </span>
  
  
          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
              
                <span class="post-meta-item-text">本文字数：</span>
              
              <span>8.4k</span>
            </span>
          
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
              
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              
              <span>8 分钟</span>
            </span>
          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h3 id="1-Makefile-介绍"><a href="#1-Makefile-介绍" class="headerlink" title="1. Makefile 介绍"></a>1. Makefile 介绍</h3><p>Makefile文件由一系列规则（rules）构成。每条规则的形式如下。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&lt;target&gt; : &lt;prerequisites&gt; </span><br><span class="line">[tab]  &lt;commands&gt;</span><br></pre></td></tr></table></figure>

<p>上面第一行冒号前面的部分，叫做”目标”（target），冒号后面的部分叫做”前置条件”（prerequisites）；第二行必须由一个tab键起首，后面跟着”命令”（commands）。</p>
<p>“目标”是必需的，不可省略；”前置条件”和”命令”都是可选的，但是两者之中必须至少存在一个。</p>
<p>每条规则就明确两件事：构建目标的前置条件是什么，以及如何构建。</p>
<a id="more"></a>

<h5 id="1-1-目标（target）"><a href="#1-1-目标（target）" class="headerlink" title="1.1 目标（target）"></a>1.1 目标（target）</h5><p>一个目标（target）就构成一条规则。目标通常是文件名，指明Make命令所要构建的对象，目标可以是一个文件名，也可以是多个文件名，之间用空格分隔。</p>
<p>除了文件名，目标还可以是某个操作的名字，这称为”伪目标”（phony target）。</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">clean:</span></span><br><span class="line">      rm *.o</span><br></pre></td></tr></table></figure>

<p>上面代码的目标是clean，它不是文件名，而是一个操作的名字，属于”伪目标 “，作用是删除对象文件。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ make  clean</span><br></pre></td></tr></table></figure>

<p>但是，如果当前目录中，正好有一个文件叫做clean，那么这个命令不会执行。因为Make发现clean文件已经存在，就认为没有必要重新构建了，就不会执行指定的rm命令。</p>
<p>为了避免这种情况，可以明确声明clean是”伪目标”，写法如下。</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><span class="meta-keyword">.PHONY</span>: clean</span></span><br><span class="line"><span class="section">clean:</span></span><br><span class="line">        rm *.o temp</span><br></pre></td></tr></table></figure>

<p>声明clean是”伪目标”之后，make就不会去检查是否存在一个叫做clean的文件，而是每次运行都执行对应的命令。</p>
<p>如果Make命令运行时没有指定目标，默认会执行Makefile文件的第一个目标。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">make</span><br></pre></td></tr></table></figure>

<h5 id="1-2-前置条件（prerequisites）"><a href="#1-2-前置条件（prerequisites）" class="headerlink" title="1.2 前置条件（prerequisites）"></a>1.2 前置条件（prerequisites）</h5><p>前置条件通常是一组文件名，之间用空格分隔。它指定了”目标”是否重新构建的判断标准：只要有一个前置文件不存在，或者有过更新（前置文件的last-modification时间戳比目标的时间戳新），”目标”就需要重新构建。</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">result.txt: source.txt</span></span><br><span class="line">    cp source.txt result.txt</span><br></pre></td></tr></table></figure>

<p>上面代码中，构建 result.txt 的前置条件是 source.txt 。如果当前目录中，source.txt 已经存在，那么<code>make result.txt</code>可以正常运行，否则必须再写一条规则，来生成 source.txt 。</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">source.txt:</span></span><br><span class="line">    echo <span class="string">"this is the source"</span> &gt; source.txt</span><br></pre></td></tr></table></figure>

<p>上面代码中，source.txt后面没有前置条件，就意味着它跟其他文件都无关，只要这个文件还不存在，每次调用<code>make source.txt</code>，它都会生成。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ make result.txt</span><br><span class="line">$ make result.txt</span><br></pre></td></tr></table></figure>

<p>上面命令连续执行两次<code>make result.txt</code>。第一次执行会先新建 source.txt，然后再新建 result.txt。第二次执行，Make发现 source.txt 没有变动（时间戳晚于 result.txt），就不会执行任何操作，result.txt 也不会重新生成。</p>
<h5 id="1-3-命令（commands）"><a href="#1-3-命令（commands）" class="headerlink" title="1.3 命令（commands）"></a>1.3 命令（commands）</h5><p>命令（commands）表示如何更新目标文件，由一行或多行的Shell命令组成。它是构建”目标”的具体指令，它的运行结果通常就是生成目标文件。</p>
<p>每行命令之前必须有一个tab键。需要注意的是，每行命令在一个单独的shell中执行。这些Shell之间没有继承关系。</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">var-lost:</span></span><br><span class="line">    <span class="keyword">export</span> foo=bar</span><br><span class="line">    echo <span class="string">"foo=[$$foo]"</span></span><br></pre></td></tr></table></figure>

<p>上面代码执行后（<code>make var-lost</code>），取不到foo的值。因为两行命令在两个不同的进程执行。一个解决办法是将两行命令写在一行，中间用分号分隔。</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">var-kept:</span></span><br><span class="line">    <span class="keyword">export</span> foo=bar; echo <span class="string">"foo=[$$foo]"</span></span><br></pre></td></tr></table></figure>

<p>另一个解决办法是在换行符前加反斜杠转义。</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">var-kept:</span></span><br><span class="line">    <span class="keyword">export</span> foo=bar; \</span><br><span class="line">    echo <span class="string">"foo=[$$foo]"</span></span><br></pre></td></tr></table></figure>

<h3 id="2-Makefile文件的语法"><a href="#2-Makefile文件的语法" class="headerlink" title="2. Makefile文件的语法"></a>2. Makefile文件的语法</h3><h5 id="2-1-井号（-）"><a href="#2-1-井号（-）" class="headerlink" title="2.1 井号（#）"></a>2.1 井号（#）</h5><p>在Makefile中表示注释。</p>
<h5 id="2-2-回声（echoing）"><a href="#2-2-回声（echoing）" class="headerlink" title="2.2 回声（echoing）"></a>2.2 回声（echoing）</h5><p>正常情况下，make会打印每条命令，然后再执行，这就叫做回声（echoing）。</p>
<p>在命令的前面加上@，就可以关闭回声。</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">test:</span></span><br><span class="line">    <span class="comment"># 这是测试</span></span><br><span class="line"></span><br><span class="line"><span class="section">test:</span></span><br><span class="line">    @<span class="comment"># 这是测试</span></span><br></pre></td></tr></table></figure>

<h5 id="2-3-通配符"><a href="#2-3-通配符" class="headerlink" title="2.3 通配符"></a>2.3 通配符</h5><p>通配符（wildcard）用来指定一组符合条件的文件名。Makefile 的通配符与 Bash 一致，主要有星号（*）、问号（？）和 […] 。比如， *.o 表示所有后缀名为o的文件。</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">clean:</span></span><br><span class="line">        rm -f *.o</span><br></pre></td></tr></table></figure>

<h5 id="2-4-模式匹配"><a href="#2-4-模式匹配" class="headerlink" title="2.4 模式匹配"></a>2.4 模式匹配</h5><p>Make命令允许对文件名，进行类似正则运算的匹配，主要用到的匹配符是%。比如，假定当前目录下有 f1.c 和 f2.c 两个源码文件，需要将它们编译为对应的对象文件。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">%.o: %.c</span><br><span class="line"></span><br><span class="line">等同于下面的写法。</span><br><span class="line"></span><br><span class="line">f1.o: f1.c</span><br><span class="line">f2.o: f2.c</span><br></pre></td></tr></table></figure>

<p>使用匹配符%，可以将大量同类型的文件，只用一条规则就完成构建。</p>
<h5 id="2-5-变量和赋值符"><a href="#2-5-变量和赋值符" class="headerlink" title="2.5 变量和赋值符"></a>2.5 变量和赋值符</h5><p>Makefile 允许使用等号自定义变量。</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">txt = Hello World</span><br><span class="line"><span class="section">test:</span></span><br><span class="line">    @echo <span class="variable">$(txt)</span></span><br></pre></td></tr></table></figure>

<p>上面代码中，变量 txt 等于 Hello World。调用时，变量需要放在 $( ) 之中。</p>
<p>调用Shell变量，需要在美元符号前，再加一个美元符号，这是因为Make命令会对美元符号转义。</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">test:</span></span><br><span class="line">    @echo $$HOME</span><br></pre></td></tr></table></figure>

<p>有时，变量的值可能指向另一个变量。</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">v1 = <span class="variable">$(v2)</span></span><br></pre></td></tr></table></figure>

<p>上面代码中，变量 v1 的值是另一个变量 v2。这时会产生一个问题，v1 的值到底在定义时扩展（静态扩展），还是在运行时扩展（动态扩展）？如果 v2 的值是动态的，这两种扩展方式的结果可能会差异很大。</p>
<p>为了解决类似问题，Makefile一共提供了四个赋值运算符 （=、:=、？=、+=），它们的区别请看<a href="http://stackoverflow.com/questions/448910/makefile-variable-assignment" target="_blank" rel="noopener">StackOverflow</a>。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">VARIABLE = value</span><br><span class="line"><span class="comment"># 在执行时扩展，允许递归扩展。</span></span><br><span class="line"></span><br><span class="line">VARIABLE := value</span><br><span class="line"><span class="comment"># 在定义时扩展。</span></span><br><span class="line"></span><br><span class="line">VARIABLE ?= value</span><br><span class="line"><span class="comment"># 只有在该变量为空时才设置值。</span></span><br><span class="line"></span><br><span class="line">VARIABLE += value</span><br><span class="line"><span class="comment"># 将值追加到变量的尾端。</span></span><br></pre></td></tr></table></figure>

<h5 id="2-6-内置变量（Implicit-Variables）"><a href="#2-6-内置变量（Implicit-Variables）" class="headerlink" title="2.6 内置变量（Implicit Variables）"></a>2.6 内置变量（Implicit Variables）</h5><p>Make命令提供一系列内置变量，比如，<code>$(CC)</code> 指向当前使用的编译器，<code>$(MAKE)</code> 指向当前使用的Make工具。这主要是为了跨平台的兼容性，详细的内置变量清单见<a href="https://www.gnu.org/software/make/manual/html_node/Implicit-Variables.html" target="_blank" rel="noopener">手册</a>。</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">output:</span></span><br><span class="line">    <span class="variable">$(CC)</span> -o output input.c</span><br></pre></td></tr></table></figure>

<h5 id="2-7-自动变量（Automatic-Variables）"><a href="#2-7-自动变量（Automatic-Variables）" class="headerlink" title="2.7 自动变量（Automatic Variables）"></a>2.7 自动变量（Automatic Variables）</h5><p>Make命令还提供一些自动变量，它们的值与当前规则有关。主要有以下几个。</p>
<ul>
<li><p><code>$@</code></p>
<p><code>$@</code>指代当前目标，就是Make命令当前构建的那个目标。比如，<code>make foo</code>的<code>$@</code> 就指代foo。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">a.txt b.txt: </span><br><span class="line">    touch <span class="variable">$@</span></span><br><span class="line">    </span><br><span class="line"><span class="comment">#等同于下面的写法。    </span></span><br><span class="line"> </span><br><span class="line">a.txt:</span><br><span class="line">    touch a.txt</span><br><span class="line">b.txt:</span><br><span class="line">    touch b.txt</span><br></pre></td></tr></table></figure>
</li>
<li><p><code>$&lt;</code></p>
<p><code>$&lt;</code> 指代第一个前置条件。比如，规则为 t: p1 p2，那么<code>$&lt;</code> 就指代p1。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">a.txt: b.txt c.txt</span><br><span class="line">    cp $&lt; <span class="variable">$@</span> </span><br><span class="line">    </span><br><span class="line"><span class="comment"># 等同于下面的写法。</span></span><br><span class="line"></span><br><span class="line">a.txt: b.txt c.txt</span><br><span class="line">    cp b.txt a.txt</span><br></pre></td></tr></table></figure>



</li>
</ul>
<ul>
<li><p><code>$?</code></p>
<p><code>$?</code> 指代比目标更新的所有前置条件，之间以空格分隔。比如，规则为 t: p1 p2，其中 p2 的时间戳比 t 新，<code>$?</code>就指代p2。</p>
</li>
</ul>
<ul>
<li><p><code>$^</code></p>
<p><code>$^</code> 指代所有前置条件，之间以空格分隔。比如，规则为 t: p1 p2，那么 <code>$^</code> 就指代 p1 p2 。</p>
</li>
</ul>
<ul>
<li><p><code>$*</code></p>
<p><code>$*</code> 指代匹配符 % 匹配的部分， 比如% 匹配 f1.txt 中的f1 ，<code>$*</code> 就表示 f1。</p>
</li>
</ul>
<ul>
<li><p><code>$(@D)</code> 和 <code>$(@F)</code></p>
<p><code>$(@D)</code> 和 <code>$(@F)</code> 分别指向 <code>$@</code> 的目录名和文件名。比如，<code>$@</code>是 src/input.c，那么<code>$(@D)</code> 的值为 src ，<code>$(@F)</code> 的值为 input.c。</p>
</li>
</ul>
<ul>
<li><p><code>$(&lt;D)</code> 和 <code>$(&lt;F)</code></p>
<p><code>$(&lt;D)</code> 和 <code>$(&lt;F)</code> 分别指向 <code>$&lt;</code> 的目录名和文件名。</p>
</li>
</ul>
<p>下面是自动变量的一个例子。</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">dest/%.txt: src/%.txt</span></span><br><span class="line">    @[ -d dest ] || mkdir dest</span><br><span class="line">    cp <span class="variable">$&lt;</span> <span class="variable">$@</span></span><br></pre></td></tr></table></figure>

<p>上面代码将 src 目录下的 txt 文件，拷贝到 dest 目录下。首先判断 dest 目录是否存在，如果不存在就新建，然后，<code>$&lt;</code> 指代前置文件（src/%.txt）， <code>$@</code> 指代目标文件（dest/%.txt）。</p>
<h5 id="2-8-判断和循环"><a href="#2-8-判断和循环" class="headerlink" title="2.8 判断和循环"></a>2.8 判断和循环</h5><p>Makefile使用 Bash 语法，完成判断和循环。</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">ifeq</span> (<span class="variable">$(CC)</span>,gcc)</span><br><span class="line">  libs=<span class="variable">$(libs_for_gcc)</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">  libs=<span class="variable">$(normal_libs)</span></span><br><span class="line"><span class="keyword">endif</span></span><br></pre></td></tr></table></figure>

<p>上面代码判断当前编译器是否 gcc ，然后指定不同的库文件。</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">LIST = one two three</span><br><span class="line"><span class="section">all:</span></span><br><span class="line">    for i in <span class="variable">$(LIST)</span>; do \</span><br><span class="line">        echo $$i; \</span><br><span class="line">    done</span><br><span class="line"></span><br><span class="line"><span class="comment"># 等同于</span></span><br><span class="line"></span><br><span class="line"><span class="section">all:</span></span><br><span class="line">    for i in one two three; do \</span><br><span class="line">        echo $i; \</span><br><span class="line">    done</span><br></pre></td></tr></table></figure>

<p>上面代码的运行结果。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">one</span><br><span class="line">two</span><br><span class="line">three</span><br></pre></td></tr></table></figure>

<h5 id="2-9-函数"><a href="#2-9-函数" class="headerlink" title="2.9 函数"></a>2.9 函数</h5><p>Makefile 还可以使用函数，格式如下。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$(<span class="keyword">function</span> arguments)</span><br><span class="line"><span class="comment"># 或者</span></span><br><span class="line"><span class="variable">$&#123;function arguments&#125;</span></span><br></pre></td></tr></table></figure>

<p>Makefile提供了许多<a href="http://www.gnu.org/software/make/manual/html_node/Functions.html" target="_blank" rel="noopener">内置函数</a>，可供调用。下面是几个常用的内置函数。</p>
<ul>
<li><p>shell 函数</p>
<p>shell 函数用来执行 shell 命令</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">srcfiles := <span class="variable">$(<span class="built_in">shell</span> echo src/&#123;00..99&#125;.txt)</span></span><br></pre></td></tr></table></figure>



</li>
</ul>
<ul>
<li><p>wildcard 函数</p>
<p>wildcard 函数用来在 Makefile 中，替换 Bash 的通配符。</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">srcfiles := <span class="variable">$(<span class="built_in">wildcard</span> src/*.txt)</span></span><br></pre></td></tr></table></figure>



</li>
</ul>
<ul>
<li><p>subst 函数</p>
<p>subst 函数用来文本替换，格式如下。</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$(<span class="built_in">subst</span> from,to,text)</span></span><br></pre></td></tr></table></figure>

<p>下面的例子将字符串”feet on the street”替换成”fEEt on the strEEt”。</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$(<span class="built_in">subst</span> ee,EE,feet on the street)</span></span><br></pre></td></tr></table></figure>



</li>
</ul>
<ul>
<li><p>patsubst函数</p>
<p>patsubst 函数用于模式匹配的替换，格式如下。</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$(<span class="built_in">patsubst</span> pattern,replacement,text)</span></span><br></pre></td></tr></table></figure>

<p>下面的例子将文件名”x.c.c bar.c”，替换成”x.c.o bar.o”。</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$(<span class="built_in">patsubst</span> %.c,%.o,x.c.c bar.c)</span></span><br></pre></td></tr></table></figure>



</li>
</ul>
<ul>
<li><p>替换后缀名</p>
<p>替换后缀名函数的写法是：变量名 + 冒号 + 后缀名替换规则。它实际上patsubst函数的一种简写形式。</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">min: $(OUTPUT:.js=.min.js)</span></span><br></pre></td></tr></table></figure>

<p>上面代码的意思是，将变量OUTPUT中的后缀名 .js 全部替换成 .min.js 。</p>
</li>
</ul>
<h3 id="3-Makefile-的实例"><a href="#3-Makefile-的实例" class="headerlink" title="3. Makefile 的实例"></a>3. Makefile 的实例</h3><h5 id="3-1-删除"><a href="#3-1-删除" class="headerlink" title="3.1  删除"></a>3.1  删除</h5><figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><span class="meta-keyword">.PHONY</span>: cleanall cleanobj cleandiff</span></span><br><span class="line"></span><br><span class="line">cleanall : cleanobj cleandiff</span><br><span class="line">        rm program</span><br><span class="line"></span><br><span class="line">cleanobj :</span><br><span class="line">        rm *.o</span><br><span class="line"></span><br><span class="line">cleandiff :</span><br><span class="line">        rm *.diff</span><br></pre></td></tr></table></figure>

<p>上面代码可以调用不同目标，删除不同后缀名的文件，也可以调用一个目标（cleanall），删除所有指定类型的文件。</p>
<h5 id="3-2-编译C语言项目"><a href="#3-2-编译C语言项目" class="headerlink" title="3.2 编译C语言项目"></a>3.2 编译C语言项目</h5><figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">edit : main.o kbd.o command.o display.o </span><br><span class="line">    cc -o edit main.o kbd.o command.o display.o</span><br><span class="line"></span><br><span class="line">main.o : main.c defs.h</span><br><span class="line">    cc -c main.c</span><br><span class="line">kbd.o : kbd.c defs.h command.h</span><br><span class="line">    cc -c kbd.c</span><br><span class="line">command.o : command.c defs.h command.h</span><br><span class="line">    cc -c command.c</span><br><span class="line">display.o : display.c defs.h</span><br><span class="line">    cc -c display.c</span><br><span class="line"></span><br><span class="line">clean :</span><br><span class="line">     rm edit main.o kbd.o command.o display.o</span><br><span class="line"></span><br><span class="line"><span class="meta"><span class="meta-keyword">.PHONY</span>: edit clean</span></span><br></pre></td></tr></table></figure>

<h5 id="3-3-项目"><a href="#3-3-项目" class="headerlink" title="3.3 项目"></a>3.3 项目</h5><figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line">.SILENT :</span><br><span class="line">.PHONY : dep vet clean dist package test</span><br><span class="line"></span><br><span class="line">NAME := cistern</span><br><span class="line">PRE := oc</span><br><span class="line">ROOF := fhyx.tech/oceans/<span class="variable">$(NAME)</span></span><br><span class="line"></span><br><span class="line">WITH_ENV = env `cat .env 2&gt;/dev/null | xargs`</span><br><span class="line">UNAME_S := <span class="variable">$(<span class="built_in">shell</span> uname -s)</span></span><br><span class="line"><span class="keyword">ifeq</span> (<span class="variable">$(UNAME_S)</span>,Darwin)</span><br><span class="line">    HOST := <span class="variable">$(<span class="built_in">shell</span> scutil --get LocalHostName)</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">    HOST := <span class="variable">$(<span class="built_in">shell</span> hostname)</span></span><br><span class="line"><span class="keyword">endif</span></span><br><span class="line"></span><br><span class="line">DATE := <span class="variable">$(<span class="built_in">shell</span> date '+%Y%m%dT%H%M')</span></span><br><span class="line">STAMP := <span class="variable">$(<span class="built_in">shell</span> date +%s)</span></span><br><span class="line">USER := <span class="variable">$(<span class="built_in">shell</span> echo $&#123;USER&#125;)</span></span><br><span class="line">TAG:=<span class="variable">$(<span class="built_in">shell</span> git describe --tags --always)</span></span><br><span class="line">LDFLAGS:=-X <span class="variable">$(ROOF)</span>/settings.Name=<span class="variable">$(NAME)</span> -X <span class="variable">$(ROOF)</span>/cmd.version=<span class="variable">$(TAG)</span> -X <span class="variable">$(ROOF)</span>/cmd.built=<span class="variable">$(DATE)</span> -X <span class="variable">$(ROOF)</span>/cmd.buildStamp=<span class="variable">$(STAMP)</span> -X <span class="variable">$(ROOF)</span>/cmd.buildUser=<span class="variable">$(USER)</span> -X <span class="variable">$(ROOF)</span>/cmd.buildHost=<span class="variable">$(HOST)</span></span><br><span class="line"></span><br><span class="line">COMMANDS = vet clean dist</span><br><span class="line"><span class="meta"><span class="meta-keyword">.PHONY</span>: $(COMMANDS)</span></span><br><span class="line"></span><br><span class="line"><span class="section">main:</span></span><br><span class="line">	echo <span class="string">"Building <span class="variable">$(NAME)</span>"</span></span><br><span class="line">	go build -ldflags <span class="string">"<span class="variable">$(LDFLAGS)</span>"</span> .</span><br><span class="line"></span><br><span class="line"><span class="section">help:</span></span><br><span class="line">	@echo <span class="string">"commands: <span class="variable">$(COMMANDS)</span>"</span></span><br><span class="line"></span><br><span class="line"><span class="section">all: clean <span class="variable">$(COMMANDS)</span></span></span><br><span class="line"></span><br><span class="line"><span class="section">vet:</span></span><br><span class="line">	echo <span class="string">"Checking ."</span></span><br><span class="line">	go vet -vettool=<span class="variable">$(which shadow)</span> -atomic -bool -copylocks -nilfunc -printf -rangeloops -unreachable -unsafeptr -unusedresult ./...</span><br><span class="line"></span><br><span class="line"><span class="section">clean:</span></span><br><span class="line">	echo <span class="string">"Cleaning dist"</span></span><br><span class="line">	rm -rf dist</span><br><span class="line">	rm -f <span class="variable">$(NAME)</span> <span class="variable">$(NAME)</span>-*</span><br><span class="line"></span><br><span class="line"><span class="section">dist/linux_amd64/$(NAME): <span class="variable">$(SOURCES)</span></span></span><br><span class="line">	echo <span class="string">"Building <span class="variable">$(NAME)</span> of linux"</span></span><br><span class="line">	mkdir -p dist/linux_amd64 &amp;&amp; GOOS=linux GOARCH=amd64 go build -ldflags <span class="string">"<span class="variable">$(LDFLAGS)</span> -s -w"</span> -o dist/linux_amd64/<span class="variable">$(PRE)</span>-<span class="variable">$(NAME)</span> <span class="variable">$(ROOF)</span></span><br><span class="line"></span><br><span class="line"><span class="section">dist/darwin_amd64/$(NAME): <span class="variable">$(SOURCES)</span></span></span><br><span class="line">	echo <span class="string">"Building <span class="variable">$(NAME)</span> of darwin"</span></span><br><span class="line">	mkdir -p dist/darwin_amd64 &amp;&amp; GOOS=darwin GOARCH=amd64 go build -ldflags <span class="string">"<span class="variable">$(LDFLAGS)</span> -w"</span> -o dist/darwin_amd64/<span class="variable">$(PRE)</span>-<span class="variable">$(NAME)</span> <span class="variable">$(ROOF)</span></span><br><span class="line"></span><br><span class="line"><span class="section">dist: clean dist/linux_amd64/<span class="variable">$(NAME)</span> dist/darwin_amd64/<span class="variable">$(NAME)</span></span></span><br><span class="line"></span><br><span class="line"><span class="section">package: dist</span></span><br><span class="line">	echo <span class="string">"Packaging <span class="variable">$(NAME)</span>"</span></span><br><span class="line">	ls dist/linux_amd64 | xargs tar -cvJf <span class="variable">$(NAME)</span>-linux-amd64-<span class="variable">$(TAG)</span>.tar.xz -C dist/linux_amd64</span><br><span class="line"></span><br><span class="line"><span class="meta"><span class="meta-keyword">.PHONY</span>: binary-deploy</span></span><br><span class="line"><span class="section">binary-deploy:</span></span><br><span class="line">	@echo <span class="string">"copy binary to earth"</span></span><br><span class="line">	@scp dist/linux_amd64/??-* earth:dist/linux_amd64/</span><br><span class="line"></span><br><span class="line"><span class="meta"><span class="meta-keyword">.PHONY</span>: package-upload</span></span><br><span class="line"><span class="section">package-upload:</span></span><br><span class="line">	@echo <span class="string">"copy package.tar.?z to venus"</span></span><br><span class="line">	@scp *-linux-amd64-*.tar.?z gopkg:/var/www/gopkg/</span><br><span class="line"></span><br><span class="line"><span class="section">docker-build: dist/linux_amd64/<span class="variable">$(NAME)</span></span></span><br><span class="line">	echo <span class="string">"Building docker image"</span></span><br><span class="line">	cp -rf Dockerfile* dist/</span><br><span class="line">	docker build -t fhyx/cistern:<span class="variable">$(TAG)</span> dist/</span><br><span class="line">	docker tag fhyx/cistern:<span class="variable">$(TAG)</span> fhyx/cistern:latest</span><br><span class="line"><span class="meta"><span class="meta-keyword">.PHONY</span>: $@</span></span><br></pre></td></tr></table></figure>

<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> cmd</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">	<span class="string">"fmt"</span></span><br><span class="line">	<span class="string">"runtime"</span></span><br><span class="line"></span><br><span class="line">	<span class="string">"github.com/spf13/cobra"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> (</span><br><span class="line">	version   = <span class="string">"dev"</span></span><br><span class="line">	built     = <span class="string">"N/A"</span></span><br><span class="line">	buildUser = <span class="string">"None"</span></span><br><span class="line">	name      = <span class="string">"cistern"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> versionCmd = &amp;cobra.Command&#123;</span><br><span class="line">	Use:   <span class="string">"version"</span>,</span><br><span class="line">	Short: <span class="string">"Print the version number"</span>,</span><br><span class="line">	Long:  <span class="string">``</span>,</span><br><span class="line">	Run: <span class="function"><span class="keyword">func</span><span class="params">(cmd *cobra.Command, args []<span class="keyword">string</span>)</span></span> &#123;</span><br><span class="line">		fmt.Printf(<span class="string">"%s %s built %s by %s (%s %s-%s)\n"</span>, name, version, built, buildUser, runtime.Version(), runtime.GOOS, runtime.GOARCH)</span><br><span class="line">	&#125;,</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">init</span><span class="params">()</span></span> &#123;</span><br><span class="line">	RootCmd.AddCommand(versionCmd)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">inDevelop</span><span class="params">()</span> <span class="title">bool</span></span> &#123;</span><br><span class="line">	<span class="keyword">return</span> version == <span class="string">"dev"</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="4-参考资料"><a href="#4-参考资料" class="headerlink" title="4. 参考资料"></a>4. 参考资料</h3><ul>
<li><a href="http://www.ruanyifeng.com/blog/2015/02/make.html" target="_blank" rel="noopener">http://www.ruanyifeng.com/blog/2015/02/make.html</a></li>
<li><a href="https://blog.csdn.net/ruglcc/article/details/7814546" target="_blank" rel="noopener">https://blog.csdn.net/ruglcc/article/details/7814546</a></li>
<li><a href="http://www.gnu.org/software/make/manual/html_node/Special-Targets.html#Special-Targets" target="_blank" rel="noopener">http://www.gnu.org/software/make/manual/html_node/Special-Targets.html#Special-Targets</a></li>
</ul>

    </div>

    
    
    
        
      
        <div id="reward-container">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作, 一个耿直的笑脸~</div>
  <button id="reward-button" disable="enable" onclick="var qr = document.getElementById(&quot;qr&quot;); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
        
      
      <div style="display: inline-block">
        <img src="/images/wechatpay.jpg" alt="Levon 微信支付">
        <p>微信支付</p>
      </div>
        
      
      <div style="display: inline-block">
        <img src="/images/alipay.jpg" alt="Levon 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>

      

      <footer class="post-footer">
          
            
          
          <div class="post-tags">
            
              <a href="/tags/makefile/" rel="tag"># makefile</a>
            
          </div>
        

        

          <div class="post-nav">
            <div class="post-nav-next post-nav-item">
              
                <a href="/p/6fa8633a.html" rel="next" title="docker中使用mongodb">
                  <i class="fa fa-chevron-left"></i> docker中使用mongodb
                </a>
              
            </div>

            <span class="post-nav-divider"></span>

            <div class="post-nav-prev post-nav-item">
              
                <a href="/p/a84d9911.html" rel="prev" title="openvpn搭建虚拟局域网">
                  openvpn搭建虚拟局域网 <i class="fa fa-chevron-right"></i>
                </a>
              
            </div>
          </div>
        
      </footer>
    
  </div>
  
  
  
  </article>

  </div>


          </div>
          
    
    
  <div class="comments" id="comments">
    <div id="disqus_thread">
      <noscript>Please enable JavaScript to view the comments powered by Disqus.</noscript>
    </div>
  </div>
  
  

        </div>
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">
        
        
        
        
      

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-Makefile-介绍"><span class="nav-number">1.</span> <span class="nav-text">1. Makefile 介绍</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#1-1-目标（target）"><span class="nav-number">1.0.1.</span> <span class="nav-text">1.1 目标（target）</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#1-2-前置条件（prerequisites）"><span class="nav-number">1.0.2.</span> <span class="nav-text">1.2 前置条件（prerequisites）</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#1-3-命令（commands）"><span class="nav-number">1.0.3.</span> <span class="nav-text">1.3 命令（commands）</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-Makefile文件的语法"><span class="nav-number">2.</span> <span class="nav-text">2. Makefile文件的语法</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#2-1-井号（-）"><span class="nav-number">2.0.1.</span> <span class="nav-text">2.1 井号（#）</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#2-2-回声（echoing）"><span class="nav-number">2.0.2.</span> <span class="nav-text">2.2 回声（echoing）</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#2-3-通配符"><span class="nav-number">2.0.3.</span> <span class="nav-text">2.3 通配符</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#2-4-模式匹配"><span class="nav-number">2.0.4.</span> <span class="nav-text">2.4 模式匹配</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#2-5-变量和赋值符"><span class="nav-number">2.0.5.</span> <span class="nav-text">2.5 变量和赋值符</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#2-6-内置变量（Implicit-Variables）"><span class="nav-number">2.0.6.</span> <span class="nav-text">2.6 内置变量（Implicit Variables）</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#2-7-自动变量（Automatic-Variables）"><span class="nav-number">2.0.7.</span> <span class="nav-text">2.7 自动变量（Automatic Variables）</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#2-8-判断和循环"><span class="nav-number">2.0.8.</span> <span class="nav-text">2.8 判断和循环</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#2-9-函数"><span class="nav-number">2.0.9.</span> <span class="nav-text">2.9 函数</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-Makefile-的实例"><span class="nav-number">3.</span> <span class="nav-text">3. Makefile 的实例</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#3-1-删除"><span class="nav-number">3.0.1.</span> <span class="nav-text">3.1  删除</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#3-2-编译C语言项目"><span class="nav-number">3.0.2.</span> <span class="nav-text">3.2 编译C语言项目</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#3-3-项目"><span class="nav-number">3.0.3.</span> <span class="nav-text">3.3 项目</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-参考资料"><span class="nav-number">4.</span> <span class="nav-text">4. 参考资料</span></a></li></ol></div>
        
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image"
      src="/images/avatar.jpg"
      alt="Levon">
  <p class="site-author-name" itemprop="name">Levon</p>
  <div class="site-description" itemprop="description">Never Give Up</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        
          <a href="/archives/">
        
          <span class="site-state-item-count">127</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
    
      
      
      <div class="site-state-item site-state-categories">
        
          
            <a href="/categories/">
          
        
        <span class="site-state-item-count">34</span>
        <span class="site-state-item-name">分类</span>
        </a>
      </div>
    
      
      
      <div class="site-state-item site-state-tags">
        
          
            <a href="/tags/">
          
        
        <span class="site-state-item-count">76</span>
        <span class="site-state-item-name">标签</span>
        </a>
      </div>
    
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="https://github.com/unix2dos" title="GitHub &rarr; https://github.com/unix2dos" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
    
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="mailto:levonfly@gmail.com" title="E-Mail &rarr; mailto:levonfly@gmail.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
    
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="https://twitter.com/levonfly" title="Twitter &rarr; https://twitter.com/levonfly" rel="noopener" target="_blank"><i class="fa fa-fw fa-twitter"></i>Twitter</a>
      </span>
    
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="https://weibo.com/l6241425" title="Weibo &rarr; https://weibo.com/l6241425" rel="noopener" target="_blank"><i class="fa fa-fw fa-weibo"></i>Weibo</a>
      </span>
    
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Levon</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    <span title="站点总字数">507k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">7:41</span>
</div>

        












        
      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js?v=3.1.0"></script>
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="/js/utils.js?v=7.4.0"></script><script src="/js/motion.js?v=7.4.0"></script>
<script src="/js/schemes/pisces.js?v=7.4.0"></script>

<script src="/js/next-boot.js?v=7.4.0"></script>



  
  <script>
    (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>








  <script src="/js/local-search.js?v=7.4.0"></script>














  

  

  

<script>
  function loadCount() {
    var d = document, s = d.createElement('script');
    s.src = 'https://levonfly.disqus.com/count.js';
    s.id = 'dsq-count-scr';
    (d.head || d.body).appendChild(s);
  }
  // defer loading until the whole page loading is completed
  window.addEventListener('load', loadCount, false);
</script>
<script>
  function disqus_config() {
    this.page.url = "https://unix2dos.github.io/p/4cf47ff4.html";
    this.page.identifier = "p/4cf47ff4.html";
    this.page.title = 'Makefile的编写规则';};
  function loadComments() {
    if (window.DISQUS) {
      DISQUS.reset({
        reload: true,
        config: disqus_config
      });
    } else {
      var d = document, s = d.createElement('script');
      s.src = 'https://levonfly.disqus.com/embed.js';
      s.setAttribute('data-timestamp', '' + +new Date());
      (d.head || d.body).appendChild(s);
    }
  }
    window.addEventListener('load', loadComments, false);
  
</script>

</body>
</html>
